package com.ruoyi.security.controller;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.security.domain.AfElevatorMaintenance;
import com.ruoyi.security.domain.AfMaintenance;
import com.ruoyi.security.domain.ElevatorInfoList;
import com.ruoyi.security.domain.ElevatorResult;
import com.ruoyi.security.domain.vo.AfElevatorMaintenanceBPVo;
import com.ruoyi.security.elevator.util.ElevatorInterfaceUtil;
import com.ruoyi.security.service.IAfElevatorMaintenanceService;
import com.ruoyi.security.service.IAfMaintenanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 电梯维保单Controller
 *
 * @author cjie
 * @date 2022-07-25
 */
@Api(tags = " 电梯维保单")
@RestController
@RequestMapping("/afElevatorMaintenance")
public class AfElevatorMaintenanceController extends BaseController {
    @Autowired
    private IAfElevatorMaintenanceService afElevatorMaintenanceService;

    @Autowired
    private RedisService redisService;

    //设备维保
    @Autowired
    private IAfMaintenanceService afMaintenanceService;

    /**
     * 查询电梯维保单列表
     */
    @ApiOperation(value="查询电梯维保单列表", notes="查询电梯维保单列表")
    @PreAuthorize(hasPermi = "security:afElevatorMaintenance:list")
    @GetMapping("/list")
    public TableDataInfo list(AfElevatorMaintenance afElevatorMaintenance) {
        startPage();
        List<AfElevatorMaintenance> list = afElevatorMaintenanceService.selectAfElevatorMaintenanceList(afElevatorMaintenance);
        return getDataTable(list);
    }

    /**
     * 导出电梯维保单列表
     */
    @ApiOperation(value="导出电梯维保单列表", notes="导出电梯维保单列表")
    @PreAuthorize(hasPermi = "security:afElevatorMaintenance:export")
    @Log(title = "电梯维保单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AfElevatorMaintenance afElevatorMaintenance) throws IOException {
        List<AfElevatorMaintenance> list = afElevatorMaintenanceService.selectAfElevatorMaintenanceList(afElevatorMaintenance);
        ExcelUtil<AfElevatorMaintenance> util = new ExcelUtil<AfElevatorMaintenance>(AfElevatorMaintenance.class);
        util.exportExcel(response, list, "电梯维保单数据");
    }
    /**
     * 获取电梯维保单详细信息
     */
    @ApiOperation(value="获取电梯维保单详细信息", notes="获取电梯维保单详细信息")
    @PreAuthorize(hasPermi = "security:afElevatorMaintenance:query")
    @GetMapping(value = "/{orderNo}")
    public AjaxResult getInfo(@PathVariable("orderNo") String orderNo) {
        return AjaxResult.success(afElevatorMaintenanceService.selectAfElevatorMaintenanceById(orderNo));
    }

    /**
     * 新增电梯维保单
     */
    @ApiOperation(value="新增电梯维保单", notes="新增电梯维保单")
    @PreAuthorize(hasPermi = "security:afElevatorMaintenance:add")
    @Log(title = "电梯维保单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AfElevatorMaintenance afElevatorMaintenance) {
        return toAjax(afElevatorMaintenanceService.insertAfElevatorMaintenance(afElevatorMaintenance));
    }

    /**
     * 修改电梯维保单
     */
    @ApiOperation(value="修改电梯维保单", notes="修改电梯维保单")
    @PreAuthorize(hasPermi = "security:afElevatorMaintenance:edit")
    @Log(title = "电梯维保单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AfElevatorMaintenance afElevatorMaintenance) {
        return toAjax(afElevatorMaintenanceService.updateAfElevatorMaintenance(afElevatorMaintenance));
    }

    /**
     * 删除电梯维保单
     */
    @ApiOperation(value="删除电梯维保单", notes="删除电梯维保单")
    @ApiImplicitParam(name = "ids", value = "电梯维保单ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "security:afElevatorMaintenance:remove")
    @Log(title = "电梯维保单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderNos}")
    public AjaxResult remove(@PathVariable String[] orderNos) {
        return toAjax(afElevatorMaintenanceService.deleteAfElevatorMaintenanceByIds(orderNos));
    }

    @ApiOperation(value="电梯维保单同步", notes="电梯维保单同步")
    @PreAuthorize(hasPermi = "security:afElevatorMaintenance:sync")
    @GetMapping("/syncInfo")
    public AjaxResult syncInfo() {

        String token = redisService.getCacheObject("elevator_token");
        if (StringUtils.isBlank(token)){
            return AjaxResult.error("梯联网token为空");
        }
        for (int page = 1;; page ++){
            ElevatorResult<ElevatorInfoList<AfElevatorMaintenance>> result = ElevatorInterfaceUtil.getMaintenanceOrderInfoList(page +"", 50 +"", token,null);
            if ("0".equals(result.getErrorCode())){
                List<AfElevatorMaintenance> list = result.getData().getRows();
                //插入或者更新
                list.forEach(o ->{
                    afElevatorMaintenanceService.insertAfElevatorMaintenance(o);
                });
                if (list.size() <50){
                    return AjaxResult.success();
                }
            }else {
                logger.error("error ==>" + result.getErrorMsg());
                return AjaxResult.error(result.getErrorMsg());
            }

        }
    }

    /**
     * 查询电梯维保次数
     */
    @ApiOperation(value="查询电梯维保次数", notes="查询电梯维保次数")
    @PreAuthorize(hasPermi = "security:afElevatorMaintenance:list")
    @GetMapping("/count")
    public AjaxResult count(AfElevatorMaintenance afElevatorMaintenance) {
//        List<AfElevatorMaintenance> list = afElevatorMaintenanceService.selectAfElevatorMaintenanceList(afElevatorMaintenance);
//        return AjaxResult.success(list.size());
        AfMaintenance maintenance = new AfMaintenance();
        maintenance.setDeviceCategory("电梯设备");
        List<AfMaintenance> list = afMaintenanceService.selectAfMaintenanceList(maintenance);
        Map<String,Integer> resultMap = new LinkedHashMap<>();
        int huoti =0;
        int keti =0;
        for(AfMaintenance afMaintenance:list){
            if(afMaintenance.getDeviceName().endsWith("货梯")){
                huoti++;
            }else {
                keti++;
            }
        }
        resultMap.put("货梯",huoti);
        resultMap.put("客梯",keti);
        return AjaxResult.success(resultMap);
    }


}
